<?php

namespace app\admin\validate;

use think\Validate;

class User extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */
	protected $rule = [
        "account"     => "require|mobile|unique:user",
        "mobile"      => "mobile|unique:user",
        "email"       => "email|unique:user",
    ];

    //验证账号格式
    public function checkFormat($param){
        //密码判断 数字、字母或下滑线   3-20位以内
        if(!preg_match("/[\w]{3,20}/", $param))return false;
        //不能是手机号码
        //if(preg_match("/1[\d]{11}/", $param))return false;

        return true;
    }

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */
    protected $message = [
        "account.require"    => '请输入登录账号',
        "account.checkFormat"=> '账号格式不正确',
        "account.unique"     => '该账号已被占用',
        "password.require"   => '请输入密码',
        "mobile.mobile"      => '手机账号格式不正确',
        "mobile.unique"      => '该手机账号已被占用',
        "email.email"        => '邮箱账号格式不正确',
        "email.unique"       => '该邮箱地址已被占用',
    ];


    //定义应用场景

    protected $scene = [
       //'login' => ['account' => 'require', 'password' => 'require'],
    ];


    //修改密码
    public function sceneEditpwd(){
        return $this->only(['password','newpwd','repwd'])
            ->remove('password', 'unique')
            ->remove('password', 'checkPwd');
    }


    //修改场景
    public function sceneUpdate(){
        return $this->only(['password'])
            ->remove('password', 'require');
    }

    //注册
    public function sceneRegist(){
        return $this->only(['account','mobile']);
    }

}
